package com.cn.wanxi.controller;

import com.cn.wanxi.entity.CompanyEntity;
import com.cn.wanxi.entity.ImgEntity;
import com.cn.wanxi.entity.ResultEntity;
import com.cn.wanxi.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/back/company")
public class CompanyController {
    @Autowired
    private ICompanyService companyService;

    @RequestMapping("/find")
    public ResultEntity find() {
        return companyService.find();
    }

    @RequestMapping("/post")
    public ResultEntity post(CompanyEntity companyEntity) {
        return companyService.update(companyEntity);
    }

    @RequestMapping("/pic")
    protected ResultEntity uploadImg(@RequestParam("file") MultipartFile multipartFile) {
        String logoName = multipartFile.getOriginalFilename();
        String fileName = UUID.randomUUID() + "--" + logoName;

        String path = "D:\\JavaProject\\step-6\\webapp\\img\\companyImg/";
        File file = new File(path + fileName);
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ResultEntity<ImgEntity> success = ResultEntity.success(new ImgEntity("/webapp/img/companyImg/" + fileName));
        success.setCode(0);
        return success;
    }
}
